package model

import (
	"github.com/wailsapp/wails"
	"sync"
)

type Storage struct {
	m     map[string]interface{}
	mutex sync.Mutex
	*wails.Runtime
	log *wails.CustomLogger
}

func (r *Storage) WailsInit(runtime *wails.Runtime) error {
	r.Runtime = runtime
	r.m = make(map[string]interface{})
	r.log = r.Log.New("Storage")
	return nil
}

func (r *Storage) Add(key string, data interface{}) {
	r.mutex.Lock()
	r.m[key] = data
	r.mutex.Unlock()
}

func (r *Storage) Delete(key string) interface{} {
	get := r.Get(key)
	if get != nil {
		r.mutex.Lock()
		defer r.mutex.Unlock()
		delete(r.m, key)
	}
	return nil
}

func (r *Storage) Get(key string) interface{} {
	r.mutex.Lock()
	defer r.mutex.Unlock()
	result, ok := r.m[key]
	if !ok {
		return nil
	}
	return result
}
